<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>
<body>
	<script>
		// 1.实现一个promise.all的接口
		function promiseAll(promiseArr) {
			return new Promise((resolve,reject)=>{
				if ( promiseArr instanceof Array == false) {return reject(new TypeError("参数必须是数组！"))};

					let resolveCount = 0;
					let resolveNum = promiseArr.length;
					let resolveValues = new Array(resolveNum);
					console.log("resolveValues=>",resolveValues)

					for (var i = 0; i <resolveNum; i++) {
						(function(i){
							Promise.resolve(promiseArr[i]).then(function(value){
								resolveCount++;
								resolveValues[i]=value;
								if ( resolveCount==resolveNum) {
									return resolve(resolveValues);
								}

							},function(err){
								return reject(err)
							})
						})(i)
					}
			})		
		}

		let p1 = Promise.resolve("p1111")
		let p2 = Promise.resolve("p2222")
		let p3 = Promise.reject("p33333")
		promiseAll([p1,p2,p3]).then(res=>{
			console.log("成功结果：",res)
		},err=>{
			console.log("失败结果：",err)
		})
	</script>
	
</body>
</html>